package com.boot.controller;

import com.boot.entity.Menu;
import com.boot.entity.User;
import com.boot.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class MenuController {
    @Autowired
    MenuService menuService;

    @GetMapping("/menu/list")
    public String list(Model model, @RequestParam(name = "current",defaultValue = "1")Integer current,
                       @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize){
        model.addAttribute("menus",menuService.list());

        model.addAttribute("menuPages",menuService.list(current,pageSize));
        model.addAttribute("pages",menuService.pages(current,pageSize));

        model.addAttribute("current",current);

        return "menu/list";
    }

    @PostMapping("/menu/deleteAll")
    @ResponseBody
    public String deleteAll(@RequestBody List<Long> ids){
        menuService.deleteAll(ids);
        return "success";
    }

    @GetMapping("/menu/delById")
    public String delById(Long id){
        menuService.delById(id);
        return "redirect:/menu/list";
    }

    @GetMapping("/menu/add")
    public String add(Model model,Long id){
        model.addAttribute("menus",menuService.list());
        if(id!=null){
            model.addAttribute("menu",menuService.getById(id));
        }
        return "menu/add";
    }

    @PostMapping("/menu/doAdd")
    @ResponseBody
    public String doAdd(@RequestBody Menu menu){
        menuService.saveOrUpdate(menu);
        return "success";
    }

}
